﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WarOfDev.EF;

namespace WarOfDev.Web.ViewModels.MappingToModel
{
    public static class UserMapping
    {
        public static void BaseToModel(User model, UserViewModel vm)
        {
            model.Id = vm.Id;
            model.IsLocked = vm.IsLocked;
            model.CreatedOn = vm.CreatedOn;
            model.CreatedBy = vm.CreatedBy;
            model.ModifiedBy = vm.ModifiedBy;
            model.ModifiedOn = vm.ModifiedOn;
            model.UserId = vm.UserId;
            model.UserName = vm.UserName;
            model.Password = vm.Password;
            model.Description = vm.Description;


            IEnumerable<UserRole> roles = vm.Roles.Split(',').Select(r => new UserRole
             {
                 Id = Guid.NewGuid().ToString(),
                 RoleId = r,
                 UserId = vm.Id,
                 CreatedOn = DateTime.Now,
                 ModifiedOn = DateTime.Now

             });

            if (!string.IsNullOrEmpty(vm.GroupId))
            {

                model.Groups.Add(new UserGroup
                {
                    Id = Guid.NewGuid().ToString(),
                    GroupId = vm.GroupId,
                    UserId = vm.Id,
                    IsLocked = vm.IsLocked,
                    CreatedOn = DateTime.Now,
                    ModifiedOn = DateTime.Now
                });
            }
        }

        public static User ToModel(this UserViewModel vm)
        {
            User model = new User();
            BaseToModel(model, vm);
            return model;
        }
    }
}